<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
    Integer surveyId = input.getInt("survey_id", -1);
    if (surveyId <= 0) {
        msg = new Message(30050, "添加失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    List<Survey> surveys = QBuilder.db("basedb").select().from("Survey").where(new Where("status = ", STATUS_ENABLE).and("id=", surveyId)).page(1, 1).query().getResultObj();
    List<SurveyQuestion> sqs = QBuilder.db("basedb").select().from("SurveyQuestion").where(new Where("status = ", STATUS_ENABLE).and("surveyId=", surveyId)).query().getResultObj();
    if (isEmpty(surveys) || isEmpty(sqs)) {
        msg = new Message(30050, "添加失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    Survey survey = surveys.get(0);
    survey.setId(null);
    survey.setHospital(hospital);
    survey.setBelong("ys"+ token.getUid());
    Integer id = (Integer)DBTools.db("basedb").addItem(survey);
    if (isEmpty(id)) {
        msg = new Message(30050, "添加失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    for (int i = 0; i < sqs.size(); i++) {
        SurveyQuestion sq = sqs.get(i);
        sq.setHospital(hospital);
        sq.setSurveyId(id);
    }
    DBTools.db("basedb").addItem(sqs);
    msg.put("data", id);
    Tools.encryptPrint(out, msg);
%>
